package com.TP.www.service.impl;

import com.TP.www.dao.impl.CouponDAO;
import com.TP.www.dao.impl.CustomerCouponDAO;
import com.TP.www.po.Coupon;
import com.TP.www.po.CustomerCoupon;
import com.TP.www.service.CouponPort;

import java.util.List;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 优惠卷功能实现类
 */
public class CouponImpl implements CouponPort {
    CouponDAO couponDAO = new CouponDAO();
    CustomerCouponDAO customerCouponDAO = new CustomerCouponDAO();
    @Override
    public void createCoupon(Integer storeId){
        String initNumString;
        String offNumString;
        double initNum;
        double offNum;
        int initNumMax = 1000;
        while (true){
            try {
                System.out.println("请输入条件金额:");
                initNumString = SYS_SC.nextLine();
                initNum = Double.parseDouble(initNumString);
                while(initNum <= 0 || initNum >= initNumMax){
                    System.out.println("输入有误!请重新输入...");
                    initNumString = SYS_SC.nextLine();
                    initNum = Double.parseDouble(initNumString);
                }
                System.out.println("请输入抵扣金额:");
                offNumString = SYS_SC.nextLine();
                offNum = Double.parseDouble(offNumString);
                while(offNum <= 0 || offNum > initNum){
                    System.out.println("输入有误!请重新输入...");
                    offNumString = SYS_SC.nextLine();
                    offNum = Double.parseDouble(offNumString);
                }
                if(offNumString.length() != 0 && initNumString.length() != 0){
                    if(couponDAO.executeUpdate(CREAT_COUPON_BY_STORE_ID,storeId,initNum,offNum) > 0){
                        System.out.println("添加成功!按0继续添加优惠卷，其他键返回...");
                        String choose = SYS_SC.nextLine();
                        if(!"0".equals(choose)){
                            return;
                        }
                    }
                }else {
                    System.out.println("添加失败....");
                    System.out.println("按任意键返回...");
                    SYS_SC.nextLine();
                }
            } catch (NumberFormatException e) {
                System.out.println("输入有误!请重新输入...");
            }
        }
    }


    @Override
    public  int getChoose(int maxNum){
        while (true){
            try{
                String num;
                System.out.println("请输入要删除的优惠卷序号:");
                num = SYS_SC.nextLine();
                while(Integer.parseInt(num) <= 0 || Integer.parseInt(num) >= maxNum){
                    System.out.println("输入有误！请重新输入....");
                    num = SYS_SC.nextLine();
                }
                return Integer.parseInt(num);
            }catch (NumberFormatException e){
                System.out.println("输入有误！请重新输入....");
            }
        }
    }


    @Override
    public void deleteCoupon(Coupon coupon){
        if(couponDAO.executeUpdate(DELETE_COUPON_BY_COUPON_ID,coupon.getId()) > 0){
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败！");
        }
        System.out.println("按任意键返回...");
        SYS_SC.nextLine();
    }


    @Override
    public void getCoupon(List<Coupon> coupons){
        List<CustomerCoupon> customerCoupons = customerCouponDAO.executeQueryF(GET_COUPON_FOR_CUSTOMER,loginUser.getId());
        if(coupons.isEmpty()){
            System.out.println("暂无优惠卷可以领取!");
        }else {
            if(customerCoupons.isEmpty()){
                for(Coupon coupon:coupons){
                    customerCouponDAO.executeUpdate(ADD_CUSTOMER_COUPON,loginUser.getId(),coupon.getId());
                }
            }else{
                for(Coupon coupon:coupons){
                    int target = 1;
                    for (CustomerCoupon customerCoupon : customerCoupons){
                        if (coupon.getId().equals(customerCoupon.getId())) {
                            target = 0;
                            break;
                        }
                    }
                    if(target == 1){
                        customerCouponDAO.executeUpdate(ADD_CUSTOMER_COUPON,loginUser.getId(),coupon.getId());
                    }
                }
            }
        }
        System.out.println("优惠卷领取完成!");
        System.out.println("按任意键返回上级...");
        SYS_SC.nextLine();
    }
}
